Graph Drawing

NetworkX does not have strong visualization capabilities. It primarily relies upon an old, and hacked together drawing function written to utilize matplotlib. This means that matplotlib drawing is slow, non-interactive, and has many visual bugs. For example, arrows for directed graphs are particularly unpleasant.

NetworkX can also use graphviz through the pygraphviz interface to read, write, and (sort of) display graphs visualized with graphviz.

For these reasons, I do not recommended that NetworkX be used as a primary tool for visualization of graphs. Visualization may be removed entirely from NetworkX in the future and moved to a separate package in the 2.0 release.

In general, I think it is a rare instance when visualizing a graph can provide meaningful information. While many graph visualizations are aesthetically pleasing, they do not convey information about the graph that could be more easily discovered by analyzing the graph using different metrics. Moreover, because positional layouts can be arbitrary, they may cause people to view structure when there is none. This is is epeciall true as graphs become larger than 100 nodes.

Regardless here are some things that illustrate the basics of graph drawing in NetworkX.


In [ ]:
import networkx as nx

This is some Jupyter magic to make the plots show up inline


In [ ]:
%matplotlib inline

In [ ]:
G = nx.barbell_graph(5,2)

In [ ]:
nx.draw(G)

In [ ]:
nx.draw(G,with_labels=True)

In [ ]:
pos = nx.layout.fruchterman_reingold_layout(G)

In [ ]:
nx.draw(G,pos=pos)

In [ ]:
nx.draw(G,pos=pos,with_labels=True)

In [ ]:
nx.draw(G,pos=pos,with_labels=True,node_color=G.nodes())

In [ ]:
G = nx.barabasi_albert_graph(15,1)
nx.draw(G,with_labels=True,node_color=G.nodes(),width=3)

In [ ]:
G = nx.random_geometric_graph(50,0.2)
kcore = nx.core_number(G)
core_color = [kcore[n] for n in G.nodes()]
pos = nx.get_node_attributes(G,'pos')
size = [3.14*G.degree(n)**2 for n in G.nodes()]

In [ ]:
nx.draw(G,node_color=core_color,lw=.5,pos=pos,node_size=size)

In [ ]:


In [ ]: